package club.koorilkaze.dao.proxy;

import club.koorilkaze.dao.IUserDAO;
import club.koorilkaze.dao.impl.UserDAOLmpl;
import club.koorilkaze.dbc.DatabaseConnection;
import club.koorilkaze.vo.User;

import java.sql.SQLException;
import java.util.ArrayList;

/**
 * @program: UserDAOProxy
 * @description: 完成数据库的打开和关闭并且调用真实实现类对象的操作
 * @author: Hermit_Yoshino
 * @create: 2019-07-24 19:29
 **/
public class UserDAOProxy implements IUserDAO {

    private DatabaseConnection databaseConnection = null;
    private IUserDAO iUserDAO = null;

    public UserDAOProxy () throws SQLException{
        try {
            databaseConnection = new DatabaseConnection();
        } catch (SQLException e) {
            throw e;
        }
        iUserDAO = new UserDAOLmpl(databaseConnection.getConnection());
    }

    @Override
    public User selectUser(String userid) throws SQLException {
        return iUserDAO.selectUser( userid );
    }

    @Override
    public ArrayList<User> selectAll() throws SQLException {
        return iUserDAO.selectAll();
    }

    @Override
    public void updata(ArrayList<User> list) throws SQLException {
        try {
            iUserDAO.updata(list);
        } catch (SQLException e) {
            throw e;
        } finally {
            try {
                databaseConnection.close();
            } catch (SQLException e) {
                throw e;
            }
        }
        return;
    }

    public void close () throws SQLException {
        try {
            databaseConnection.close();
        } catch (SQLException e) {
            throw e;
        }
    }
}
